//
//  BaseViewController.m
//  BaseProject
//
//  Created by 喻飞 on 15/5/10.
//  Copyright (c) 2015年 yuf. All rights reserved.
//

#import "BaseViewController.h"
#import "MyNavigationViewController.h"

@interface BaseViewController ()

@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;
@end

@implementation BaseViewController
@synthesize  shareApi;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //        naviItem = [[CustomNaviItem alloc] init];
        //        [self.navigationItem setHidesBackButton:YES];
        //        naviItem.delegate = self;
        
        _allowSwipeGesture = YES;
        
    }
    return self;
}
- (void)awakeFromNib
{
    [super awakeFromNib];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    [self dismissWaitDialog];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [UIViewController attemptRotationToDeviceOrientation];

    // Do any additional setup after loading the view.
   [self.view setBackgroundColor:[UIColor whiteColor]];
    shareApi=[LQRequestApi sharedInstance];
    shareApi.delegate=self;
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(ProductTypeChoice:) name:Notification_ProductTypeChoice object:nil];
    [self.view addGestureRecognizer:self.rightSwipeGesture];
    //键盘显示，隐藏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)ProductTypeChoice:(NSNotification *)notify{
    
}

#pragma mark  调整自适应布局
- (void) adaptiveViewLayout:(UIView *)viewLayout{
    //视图自适应布局
    for (UIView *view in viewLayout.subviews) {
        view.frame = flexibleFrame(view.frame, NO);
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *viewField = (UITextField *)view;
            CGFloat fontSize = viewField.font.pointSize;
            viewField.font = [UIFont systemFontOfSize:fontSize*VerticalTexeRatio()];
        }else if ([view isKindOfClass:[UILabel class]]){
            UILabel *viewLabel = (UILabel *)view;
            CGFloat fontSize = viewLabel.font.pointSize;
            viewLabel.font = [UIFont systemFontOfSize:fontSize*VerticalTexeRatio()];
        }else if ([view isKindOfClass:[UIButton class]]){
            UIButton *viewButton = (UIButton *)view;
            CGFloat fontSize = viewButton.titleLabel.font.pointSize;
            viewButton.titleLabel.font = [UIFont systemFontOfSize:fontSize*VerticalTexeRatio()];
        }
        
    }
}
#pragma mark - Custom mehtod
- (void)hideKeyBoard{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}


#pragma mark 登陆界面======================================================
- (void)ShowLoginPage
{
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Login"];
    MyNavigationViewController * navC = (MyNavigationViewController *)self.navigationController;
    [navC pushToViewController:page ];
}

-(void)showWaitDialogForNetWork
{
    [self performSelector:@selector(showWaitDialogForWait:) withObject:nil afterDelay:0.001];
}
- (void)showWaitDialog:(NSString*)title state:(NSString*)state{
    
    [CWHUDTips showTips:title];
    
}
- (void)showWaitDialogForWait:(NSString *)str {
    
    [CWHUDTips showLoadingTips:@"加载中..."];
}

-(void)showWaitDialogForNetWorkDismissBySelf
{
    [self showWaitDialogForNetWork];
    double delayInSeconds = 1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self dismissWaitDialog];
    });
    
}

-(void)action_swipeGesture:(UISwipeGestureRecognizer *)gesture{
    if (gesture.direction == UISwipeGestureRecognizerDirectionRight){
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}



-(void)dismissWaitDialog
{
    [CWHUDTips hideTips];
    
}

- (UISwipeGestureRecognizer *)rightSwipeGesture
{
    if (!_rightSwipeGesture) {
        _rightSwipeGesture = ({
            UISwipeGestureRecognizer * rightSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(action_swipeGesture:)];
            rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
            
            rightSwipeGesture;
        });
    }
    return _rightSwipeGesture;
}
#pragma mark - 键盘事件=======================================
-(void)Actiondo:(id)sender{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self.view removeGestureRecognizer:self.tapGesture];
    self.tapGesture = nil;
}

-(void)keyboardWillHide:(NSNotification *)notification
{
    //    NSLog(@"*-----HideKeyBoard");
    [self.view removeGestureRecognizer:self.tapGesture];
    self.tapGesture = nil;
}

-(void)keyboardWillShow:(NSNotification *)notification
{
    //    NSLog(@"*-----ShowKeyBoard");
    if (self.tapGesture) {
        [self.view removeGestureRecognizer:self.tapGesture];
        self.tapGesture = nil;
    }
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Actiondo:)];
    [self.view addGestureRecognizer:self.tapGesture];
}
#pragma mark - 会员浏览商品=======================================
- (void)userScanProduct:(NSString *)goodsId{
    if ([Common getLoginStatus]) {
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        [dic setObject:goodsId forKey:@"productId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_saveAppProductScan withHttpType:@"POST" withTag:SX_saveAppProductScanTag];
}

}


#pragma  mark 横屏设置
//强制横屏
- (void)forceOrientationLandscape
{
    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.isForceLandscape=YES;
    appdelegate.isForcePortrait=NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}

//强制竖屏
- (void)forceOrientationPortrait
{
    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.isForcePortrait=YES;
    appdelegate.isForceLandscape=NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}


- (BOOL)prefersStatusBarHidden{
    return YES;
}

@end
